package com.example.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {

    /**
     * 获取连接
     */
    public static Connection getConnection() throws Exception {
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driverClass = properties.getProperty("driverClass");

        Class.forName(driverClass);
        return DriverManager.getConnection(url, user, password);
    }

    // 创建c3p0连接池
    private static final ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("hello-c3p0");

    /**
     * 使用C3P0连接池获取连接
     */
    public static Connection getConnectionWithC3p0() throws Exception {
        // 获取连接
        return comboPooledDataSource.getConnection();
    }

    private static BasicDataSource basicDataSource;

    static {
        try {
            // 创建dbcp连接池
            Properties properties = new Properties();
            properties.load(Files.newInputStream(Paths.get("src/main/resources", "dbcp.properties")));
            basicDataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * 使用DBCP连接池获取连接
     */
    public static Connection getConnectionWithDbcp() throws Exception {
        // 获取连接
        return basicDataSource.getConnection();
    }

    private static DataSource druidDataSource;

    static {
        try {
            // 创建druid连接池
            Properties properties = new Properties();
            properties.load(Files.newInputStream(Paths.get("src/main/resources", "druid.properties")));
            druidDataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * 使用Druid连接池获取连接
     */
    public static Connection getConnectionWithDruid() throws Exception {
        // 获取连接
        return druidDataSource.getConnection();
    }

    /**
     * 关闭资源
     */
    public static void closeResource(Connection connection, Statement statement) {
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 关闭资源
     */
    public static void closeResource(Connection connection, Statement statement, ResultSet resultSet) {
        closeResource(connection, statement);

        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 关闭资源
     */
    public static void closeResourceWithDbUtils(Connection connection, Statement statement, ResultSet resultSet) {
        // try {
        //     DbUtils.close(connection);
        //     DbUtils.close(statement);
        //     DbUtils.close(resultSet);
        // } catch (SQLException exception) {
        //     exception.printStackTrace();
        // }

        DbUtils.closeQuietly(connection);
        DbUtils.closeQuietly(statement);
        DbUtils.closeQuietly(resultSet);
    }

}
